const express = require('express')
const router = express.Router()
var request = require('request')
const asyncHandler = require('../util/asyncHandler')
const fs = require('fs')

router.get('/remove', function(req, res) {
    request.post(
        {
            url: 'https://api.remove.bg/v1.0/removebg',
            formData: {
                image_file: fs.createReadStream('./server/ztest/file.jpg'),
                size: 'auto'
            },
            headers: {
                'X-Api-Key': 'eBuH8yqyZr6XxxkQsyk3qk4A'
            },
            encoding: null
        },
        function(error, response, body) {
            if (error) return console.error('Request failed:', error)
            if (response.statusCode != 200)
                return console.error(
                    'Error:',
                    response.statusCode,
                    body.toString('utf8')
                )
            fs.writeFileSync('./server/ztest/no-bg.png', body)
        }
    )

    res.end('111111111111111')
})

module.exports = router
